function getNextElement(el) {
  var nextEl = el.nextSibling;
  while (nextEl && nextEl.nodeType != 1) {
    nextEl = nextEl.nextSibling;
  }
  return nextEl;
}

var previousItemHead;
var previousTarget;
var commentBoxPopup;
var commentBoxPopupContent;

function toggleItem() {
  var previousItemBody;
  if (previousItemHead) {
    previousItemBody = getNextElement(previousItemHead);
    previousItemBody.style.display = 'none';
  }
  var itemBody = getNextElement(this);
  if (itemBody == previousItemBody) {
    commentBoxPopup.style.display = 'none';
    itemBody.style.display = 'none';
    previousItemHead = null;
  } else {
    itemBody.style.display = 'block';
    previousItemHead = this;
  }
}

function showCommentBoxPopup(event, win) {
  win = win || window;
  var doc = win.document;
  event = event || win.event;
  var target = event.target || event.srcElement;
  if (target.tagName != 'P') return;
  if (previousTarget && previousTarget != target) previousTarget.className = '';
  target.className = 'active';
  var commentBox = getNextElement(target);
  if (commentBox && commentBox.className == 'commentBox') {
    commentBoxPopup.style.display = 'block';
    commentBoxPopupContent.innerHTML = commentBox.innerHTML;
    commentBoxPopup.style.top = parseInt(target.offsetTop - commentBoxPopupContent.offsetHeight / 2) + 'px';
    commentBoxPopup.style.height = commentBoxPopupContent.offsetHeight + 'px';
  } else {
    commentBoxPopup.style.display = 'none';
  }
  previousTarget = target
}

function bodyOnload(){
  var urightColumn = document.getElementById('urightColumn');
  var children = urightColumn.children;
  var itemHeads = [];
  var itemBodies = [];
  for (i = 0; i < children.length; i++) {
    switch (children[i].className) {
      case 'itemHead':
        itemHeads.push(children[i]);
        break;
      case 'itemBody':
        itemBodies.push(children[i]);
        break;
    }
  }
  for (i = 0; i < itemHeads.length; i++) {
    itemHeads[i].onclick = toggleItem;
  }
  for (i = 0; i < itemBodies.length; i++) {
    itemBodies[i].onmouseover = showCommentBoxPopup;
  }
  
  commentBoxPopup = document.getElementById('commentBoxPopup');
  commentBoxPopupContent = document.getElementById('commentBoxPopupContent');
}

function createMarker(point, description) {
  var marker = new GMarker(point);
  marker.openInfoWindowHtml(description);
  return marker;
}

function showMap()
{
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));                
    map.addControl(new GSmallMapControl());	
    var pos = new GLatLng(45.400996,11.942804);
    map.setCenter(pos, 11);
    map.addOverlay(createMarker(pos, "<p><b>Vetlab s.n.c.</b></p><p>Via Provenza, 20</p><p>35127 Padova</p><p>Tel. e Fax: 049/8702559</p>"));
  }
}

window.addEvent('domready', function(){
  var toggle = function(e, expand){
    e.stop();
    var t = document.id(e.target).setStyle('display', 'none');
    t[expand ? 'getNext' : 'getPrevious']().setStyle('display', 'inline');
  };
  $$('.itemHead span').each(function(collapse){
    var expand = new Element('span', {text: ' (...)'})
      .addClass('expand')
      .injectBefore(collapse)
      .addEvent('click', toggle.bindWithEvent(null, true));
    collapse.addEvent('click', toggle.bindWithEvent(null, false));
  });

  var blink = document.id('blink');
  if (blink){
    (function(){
      blink.toggleClass('off');
    }).periodical(800); 
  }
});
